package com.uinnova.springboot.mybatis;


import com.github.pagehelper.PageHelper;
import com.uinnova.springboot.mapper.SysUserMapper;
import com.uinnova.springboot.model.SysUser;
import com.uinnova.springboot.mybatis.service.SysUserService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/*
*@author 王刚
*@date 2019-01-07 11:02
*
*/
@RestController
@Api
public class MybatisController {
    @Autowired
   private SysUserMapper sysUserMapper;

    @Autowired
    private SysUserService sysUserService;

    /*
    *@author 王刚
    *@date 2019-01-07 14:53
    *测试单表的增删改查并且集成cache缓存
    */

//    增加用户
    @GetMapping(value = "/jail/mybatis/adduser")
    public void addUser(){
        SysUser sysUser1 = new SysUser(1,"wanggang1","1");
        SysUser sysUser2 = new SysUser(2,"wanggang2","2");
        SysUser sysUser3 = new SysUser(3,"wanggang3","3");
        SysUser sysUser4 = new SysUser(4,"wanggang4","4");
        sysUserService.addSysUser(sysUser1);
        sysUserService.addSysUser(sysUser2);
        sysUserService.addSysUser(sysUser3);
        sysUserService.addSysUser(sysUser4);
    }

//    删除用户
    @GetMapping(value = "/jail/mybatis/deleteuser/{id}")
    public void deleteUser(@PathVariable Integer id){
        sysUserService.deleteSysUser(id);
    }

//    修改用户信息
    @GetMapping(value = "/jail/mybatis/updateuser/{id}")
    public void updateUser(@PathVariable Integer id){
        SysUser sysUser = sysUserService.queryById(id);
        sysUser.setSysName("wanggangxiugai");
        sysUser.setSysAge("22222");
        sysUserService.updateSysUser(sysUser);
    }

//    查询用户信息
    @GetMapping(value = "/jail/mybatis/queryuser/{id}")
    public SysUser queryUser(@PathVariable Integer id){
        SysUser sysUser = sysUserService.queryById(id);
        return sysUser;
    }

/*
*测试查询全部用户
*/
    @GetMapping(value = "/jail/mybatis/user/{id}/info")
    public List<SysUser> listUserById(@PathVariable String id){
        return sysUserMapper.listUserById(id);

    }

    /*
    *测试pageHelpaer分页
    */
    @GetMapping(value = "/jail/mybatis/page/{pageNum}/{pageSize}")
    public List<SysUser> sysUserListPage(@PathVariable Integer pageNum, @PathVariable Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        return sysUserMapper.selectAll();
    }

}
